﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Common;
using ExpenseSystem.DataUpdater.Validation;

namespace ExpenseSystem.Core.ProductionModules
{
    public class ProductionUpdaterRegister
    {
        int rows;
        public NormalDataUpdater<ProductionModel> GetProductionUpdater()
        {
            NormalDataUpdater<ProductionModel> systemDataUpdater = new NormalDataUpdater<ProductionModel>();
            systemDataUpdater.DataUpdating += DataNotSpaceValid;
            systemDataUpdater.DataUpdating += SingletonValid;
            systemDataUpdater.DataUpdating += DocExist;
            systemDataUpdater.DataDeleting += CheckAvaliable;
            return systemDataUpdater;
        }
        private void CheckAvaliable(object sender, DataValidationEventArgs<ProductionModel> e)
        {
            QuoteDetailFetcher quoteDetailFetcher = new QuoteDetailFetcher();
            OrderDetailFetcher orderDetailFetcher = new OrderDetailFetcher();
            string message;
            if(quoteDetailFetcher.ListByProduction(e.Delete.Site,e.Delete.AutoId,out message, out rows).Count>0
                ||orderDetailFetcher.ListByProduction(e.Delete.Site,e.Delete.AutoId,out message, out rows).Count>0)
            {
                e.Message = "该档案已经被使用";
                e.Cancle = true;
                return;
            }
        }

        private void SingletonValid(object sender, DataValidationEventArgs<ProductionModel> e)
        {
            ProductionFetcher ProductionFetcher = new ProductionFetcher();
            string message = "";
            ProductionModel Production = ProductionFetcher.GetByCode( e.Insert.Site, e.Insert.Code, out message);
            if (Production != null && Production.AutoId != e.Insert.AutoId)
            {
                e.Cancle = true;
                message = String.Format("代号[{0}]已被[{1}]占用", Production.Code, Production.Name);
                return;
            }
        }

        private void DataNotSpaceValid(object sender, DataValidationEventArgs<ProductionModel> e)
        {
            if (String.IsNullOrWhiteSpace(e.Insert.Code))
            {
                e.Message = "代号不能为空";
                e.Cancle = true;
                return;
            }
            if (String.IsNullOrWhiteSpace(e.Insert.Name))
            {
                e.Message = "名称不能为空";
                e.Cancle = true;
                return;
            }
        }
        private void DocExist(object sender,DataValidationEventArgs<ProductionModel> e)
        {
            string message;
            UnitFetcher unitFetcher = new UnitFetcher();
            IList<UnitModel> models = unitFetcher.ListByMainId(e.Insert.Site, e.Insert.UnitGroupId, out message, out rows);
            var unit = from model in models where model.AutoId == e.Insert.UnitId select model;
            var unit2 = from model in models where model.AutoId == e.Insert.SalesUnitId select model;
            if(unit.Count()+unit2.Count()!=2)
            {
                e.Message = "单位档案不存在";
                e.Cancle = true;
                return;
            }
            ProductionTypeFetcher productionTypeFetcher = new ProductionTypeFetcher();
            SeriesFetcher seriesFetcher = new SeriesFetcher();
            BlandFetcher blandFetcher = new BlandFetcher();
            if(productionTypeFetcher.Get(e.Insert.Site,e.Insert.ProductionTypeId,out message)==null||
                seriesFetcher.Get(e.Insert.Site,e.Insert.SeriesId,out message) == null ||
                blandFetcher.Get(e.Insert.Site,e.Insert.BlandId,out message) == null)
            {
                e.Message = "系列/品牌/类型档案不存在";
                e.Cancle = true;
                return;
            }
        }
    }
}
